{
 "metadata": {
  "name": "",
  "signature": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import os.path as osp\n",
      "\n",
      "import numpy as np\n",
      "import matplotlib.pyplot as plt\n",
      "%matplotlib inline\n",
      "import scipy.linalg as la\n",
      "\n",
      "import logging\n",
      "logging.getLogger().setLevel(logging.DEBUG)\n",
      "\n",
      "import openmodes\n",
      "import openmodes.basis\n",
      "from openmodes.operator import EfieOperator, MfieOperator\n",
      "from openmodes.operator.mfie import TMfieOperator\n",
      "from openmodes.sources import PlaneWaveSource\n",
      "from openmodes.integration import DunavantRule"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": ""
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from openmodes.ipython import init_3d\n",
      "init_3d()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": ""
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "basis_class = openmodes.basis.DivRwgBasis\n",
      "#basis_class = openmodes.basis.LoopStarBasis\n",
      "\n",
      "\n",
      "name = 'sphere'\n",
      "\n",
      "parameters = {'radius': 5e-3, 'mesh_tol': 2e-3}\n",
      "\n",
      "sim_efie = openmodes.Simulation(name=\"EFIE\", operator_class=EfieOperator,\n",
      "                                basis_class=basis_class)\n",
      "sim_mfie = openmodes.Simulation(name=\"MFIE\", operator_class=MfieOperator,\n",
      "                                basis_class=basis_class)\n",
      "\n",
      "mesh = sim_efie.load_mesh(osp.join(openmodes.geometry_dir, name+'.geo'),\n",
      "                          parameters=parameters)\n",
      "sim_efie.place_part(mesh)\n",
      "sim_mfie.place_part(mesh)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": ""
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#num_freqs = len(freqs)\n",
      "\n",
      "#extinction_efie = np.empty(num_freqs, np.complex128)\n",
      "#extinction_mfie = np.empty(num_freqs, np.complex128)\n",
      "\n",
      "e_inc = np.array([1, 0, 0], dtype=np.complex128)#/np.sqrt(2)\n",
      "k_hat = np.array([0, 0, 1], dtype=np.complex128)\n",
      "\n",
      "pw = PlaneWaveSource(e_inc, k_hat)\n",
      "\n",
      "\n",
      "s= 2j*np.pi*1e9\n",
      "Z_efie = sim_efie.impedance(s)\n",
      "V_efie = sim_efie.source_vector(pw, s)\n",
      "I_efie = Z_efie.solve(V_efie)\n",
      "\n",
      "Z_mfie = sim_mfie.impedance(s)\n",
      "V_mfie = sim_mfie.source_vector(pw, s)\n",
      "I_mfie = Z_mfie.solve(V_mfie)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": ""
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#%debug\n",
      "from numpy import savetxt\n",
      "savetxt('nodes.txt', mesh.nodes)\n",
      "print mesh.polygons[0], mesh.polygons[96]\n",
      "print mesh.surface_normals[0]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": ""
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sim_efie.plot_3d(I_efie*100, output_format='webgl')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#V2 = sim_mfie.empty_vector()\n",
      "#V2[:] = la.solve(Z_mfie[:].basis_o.gram_matrix, V_mfie)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sim_mfie.plot_3d(I_mfie*100, output_format='webgl')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Confirm that the absolute magnitude of the EFIE and MFIE solutions also agree"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "plt.figure(figsize=(10, 5))\n",
      "plt.plot(abs(I_efie), 'x')\n",
      "plt.plot(abs(I_mfie), '+')\n",
      "plt.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}